package com.jiyi.retrofit;

import android.util.Log;

import com.google.gson.Gson;
import com.jiyi.bean.Result;
import com.jiyi.bean.iot.FireAlarmBean;
import com.jiyi.exception.ExceptionEngine;
import com.jiyi.exception.ServerException;
import com.swift.maintenance.base.AppConfig;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 12262 on 2016/5/31.
 */
public class IotHttpMethods {
    public static final String TAG = IotHttpMethods.class.getName();
    public static final String BASE_URL = AppConfig.IOT_HOST;

    private static final int DEFAULT_TIMEOUT = 5;

    private Retrofit retrofit;
    private IotService mIotService;

    //构造方法私有
    private IotHttpMethods() {
        Log.e(TAG," 创建 IotHttpMethods  ");
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        mIotService = retrofit.create(IotService.class);
    }

    //在访问HttpMethods时创建单例
    private static class SingletonHolder{
        private static final IotHttpMethods INSTANCE = new IotHttpMethods();
    }

    //获取单例
    public static IotHttpMethods getInstance(){
        return SingletonHolder.INSTANCE;
    }

    private  class  ServerResultFunc<T> implements Function<Result<T>, T>
    {
        @Override
        public T apply(Result<T> httpResult) throws Exception {
            if (httpResult.status != 200) {
                Log.e(TAG,"ServerResultFunc status  "+httpResult.status+"   data  "+ httpResult.data);
                throw new ServerException(httpResult.status,httpResult.info);
            }
            Log.e(TAG,"httpResult.data:"+ new Gson().toJson(httpResult.data));
            return httpResult.data;
        }
    }

    private class HttpResultFunc<T> implements Function<Throwable, Observable<T>> {

        @Override
        public Observable<T> apply(Throwable throwable) throws Exception {
            Log.e(TAG,"HttpResultFunc" + throwable.getMessage());
            return Observable.error(ExceptionEngine.handleException(throwable));
        }
    }


    public Observable<List<FireAlarmBean>> getFireAlarmList(String token, int type, int p){
        return mIotService.getFireAlarmList(token, type,p)
                .map(new IotHttpMethods.ServerResultFunc<List<FireAlarmBean>>())
                .subscribeOn(Schedulers.io())
                .onErrorResumeNext(new IotHttpMethods.HttpResultFunc<List<FireAlarmBean>>())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<FireAlarmBean>> getFireAlarmList(int type){
        return mIotService.getFireAlarmList(type)
                .map(new IotHttpMethods.ServerResultFunc<List<FireAlarmBean>>())
                .subscribeOn(Schedulers.io())
                .onErrorResumeNext(new IotHttpMethods.HttpResultFunc<List<FireAlarmBean>>())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<FireAlarmBean> getFireAlarm(int type,String alarm_sn){
        Log.e(TAG, "getFireAlarm: type "+type +" sn " +alarm_sn);
        return mIotService.getFireAlarm(type,alarm_sn)
                .map(new IotHttpMethods.ServerResultFunc<FireAlarmBean>())
                .subscribeOn(Schedulers.io())
                .onErrorResumeNext(new IotHttpMethods.HttpResultFunc<FireAlarmBean>())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
